package demo.huodongbaoming.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "users")
@EqualsAndHashCode(exclude = {"registeredActivities", "createdActivities"})
@ToString(exclude = {"registeredActivities", "createdActivities"})
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String username;

    @Column(nullable = false)
    private String password;

    private String name;
    
    private String email;
    
    private String phone;
    
    private String studentId;
    
    @Column(name = "is_admin")
    private Boolean isAdmin = false;
    
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
        name = "user_activities",
        joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "activity_id")
    )
    private Set<Activity> registeredActivities = new HashSet<>();
    
    @OneToMany(mappedBy = "creator", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<Activity> createdActivities = new HashSet<>();

    public void setAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }
}